package com.wushi.lenovo.asia5b.app_utils.eye;

import android.content.Context;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.ListPopupWindow;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.PopupWindow;

import com.wushi.lenovo.asia5b.R;

/**
 * Created by zhangyuncai on 2019/1/20.
 */
public class PopupWindowHelper {
    public static ListPopupWindow getListPopupWindow(Context context, ArrayAdapter adapter, View anchorView, int height) {
        ListPopupWindow listPopupWindow = new ListPopupWindow(context);
        listPopupWindow.setWidth(ListPopupWindow.WRAP_CONTENT);
//        listPopupWindow.setHeight(DisplayUtils.dip2px(context, 150));
        listPopupWindow.setHeight(height);
        listPopupWindow.setAnchorView(anchorView);
        listPopupWindow.setModal(true);
        listPopupWindow.setAdapter(adapter);
        return listPopupWindow;
    }

    public static void showPopupWindow(ListPopupWindow listPopupWindow) {
        listPopupWindow.show();
        ListView listView = listPopupWindow.getListView();
        if (listView != null) {
            listView.setDividerHeight(0);
        }
    }

    public static PopupWindow getPopupWindow(Context context, PopupItemAdapter adapter) {
        View popupView = View.inflate(context, R.layout.popup_list, null);
        RecyclerView recyclerView = (RecyclerView) popupView.findViewById(R.id.recyclerview_popup);
        recyclerView.setLayoutManager(new LinearLayoutManager(context));
        recyclerView.setAdapter(adapter);
        PopupWindow popupWindow = new PopupWindow(popupView, ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
        popupWindow.setOutsideTouchable(true);
        return popupWindow;
    }
}
